package com.dongdongshop.service.impl;

import com.dongdongshop.common.ShiroUtils;
import com.dongdongshop.mapper.TbUserMapper;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.pojo.TbUserExample;
import com.dongdongshop.service.TbUserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@DubboService
public class TbUserServiceImpl implements TbUserService {
    @Resource
    private TbUserMapper tbUserMapper;

    @Override
    public int insertTbUser(TbUser user) {
        String salt = ShiroUtils.generateSalt(6);/*生成6位数的盐*/
        String md5 = ShiroUtils.encryptPassword("MD5", user.getPassword(), salt,3);
        user.setSalt(salt);
        user.setPassword(md5);
        user.setCreated(new Date());
        int i = tbUserMapper.insertSelective(user);
        return i;
    }

    @Override
    public TbUser selectByUserName(String username) {
        TbUserExample example = new TbUserExample();
        TbUserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<TbUser> tbUsers = tbUserMapper.selectByExample(example);
        TbUser user = tbUsers.get(0);
        return tbUsers == null || tbUsers.size() == 0 ? null : user;
    }
}
